package com.heima.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.admin.mapper.AdChannelMapper;
import com.heima.admin.service.AdChannelService;
import com.heima.model.admin.dtos.ChannelDto;
import com.heima.model.admin.pojos.AdChannel;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * @author zhanghao
 * @date 2021/4/9 17:06
 */
@Service
@Transactional
public class AdChannelServiceImpl extends ServiceImpl<AdChannelMapper, AdChannel> implements AdChannelService {
    //根据名称分页查询频道
    @Override
    public ResponseResult findByNameAndPage(ChannelDto dto) {
        //1.检查参数
        if (dto == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        dto.checkParam();
        //2.查询条件封装
        //分页条件
        Page<AdChannel> page = new Page<>(dto.getPage(), dto.getSize());
        //name 模糊查询
        LambdaQueryWrapper<AdChannel> queryWrapper = Wrappers.lambdaQuery();
        if (StringUtils.isNotBlank(dto.getName())) {
            queryWrapper.like(AdChannel::getName, dto.getName());
        }
        //3.执行分页查询
        IPage<AdChannel> pageResult = page(page, queryWrapper);
        //4.封装结果返回
        PageResponseResult pageResponseResult = new PageResponseResult(dto.getPage(), dto.getSize(), (int) pageResult.getTotal());
        pageResponseResult.setData(pageResult.getRecords());
        return pageResponseResult;
    }

    //频道新增
    @Override
    public ResponseResult insert(AdChannel adChannel) {
        //1.检查参数
        if (adChannel == null || StringUtils.isBlank(adChannel.getName())) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        if (adChannel.getIsDefault() == null) {
            adChannel.setIsDefault(false);
        }
        //2.执行新增
        adChannel.setCreatedTime(new Date());
        if (adChannel.getIsDefault()) {
            // 查询原有的默认的频道，更新为非默认
            LambdaQueryWrapper<AdChannel> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(AdChannel::getIsDefault, 1);
            AdChannel one = getOne(queryWrapper);
            //判断是存在默认频道
            if (one != null) {
                one.setIsDefault(false);
                updateById(one);
            }
        }
        save(adChannel);
        //返回结果
        return ResponseResult.okResult();
    }


    //频道修改
    @Override
    public ResponseResult update(AdChannel adChannel) {
        //1.检查参数
        if (adChannel == null || adChannel.getId() == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        if (adChannel.getIsDefault() == null) {
            adChannel.setIsDefault(false);
        }
        //2.先查后改
        AdChannel channel = getById(adChannel.getId());
        if (channel == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        // 查询原有的默认的频道，更新为非默认
        if (adChannel.getIsDefault()) {
            LambdaQueryWrapper<AdChannel> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(AdChannel::getIsDefault, 1);
            AdChannel one = getOne(queryWrapper);
            if (one != null) {
                one.setIsDefault(false);
                updateById(one);
            }
        }
        //3.返回结果
        updateById(adChannel);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    //频道删除
    @Override
    public ResponseResult deleteById(Integer id) {
        //1.检查参数
        if (id == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
            //throw new CustomException(AppHttpCodeEnum.PARAM_INVALID);
        }
        //2.判断当前频道是否存在，是否有效
        AdChannel adChannel = getById(id);
        if (adChannel == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
            //throw new CustomException(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        //3.启用状态无法删除
        if (adChannel.getStatus()) {
            return ResponseResult.errorResult(AppHttpCodeEnum.CHANNEL_USED);
        }
        removeById(id);
        //4.返回结果
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
